import Vue from 'vue';
import Router from 'vue-router';
import IndexPage from './components/IndexPage.vue';
import CenterPage from './components/CenterPage.vue';
import LocalPage from './components/LocalPage.vue';
import MessagePage from './components/MessagePage.vue';

Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'IndexPage',
      component: IndexPage
    },
    {
      path: '/center',
      name: 'CenterPage',
      component: CenterPage
    },
    {
      path: '/local',
      name: 'LocalPage',
      component: LocalPage
    },
    {
      path: '/message',
      name: 'MessagePage',
      component: MessagePage
    }
  ],
  mode: 'hash',
  // 简化滚动行为配置，确保每次切换路由都从顶部开始
  scrollBehavior() {
    // 无论是否有保存的位置，都强制滚动到顶部
    return { x: 0, y: 0 };
  }
});

// 额外保障：某些页面使用内部可滚动容器，切换路由后强制把这些容器也回到顶部
router.afterEach(() => {
  // 使用微任务/宏任务确保在页面渲染后执行
  Vue.nextTick(() => {
    setTimeout(() => {
      try {
        window.scrollTo(0, 0);
        document.documentElement.scrollTop = 0;
        document.body.scrollTop = 0;
      } catch (e) { void e; }

      const selectors = [
        '.index-page',
        '.center-page',
        '.local-page',
        '.message-page',
        '.mults-scroll-inner',
        '.swipeable-item',
        '.content-area',
        '[style*="overflow"]'
      ];

      selectors.forEach((sel) => {
        document.querySelectorAll(sel).forEach((el) => {
          try {
            el.scrollTop = 0;
          } catch (e) { void e; }
        });
      });
    }, 0);
  });
});

export default router;
